﻿package net.wangit.view.element.component;

import net.wangit.Page;
import net.wangit.Row;
import net.wangit.context.ActionContext;
import net.wangit.support.ElementWriter;
import net.wangit.util.Checker;
import net.wangit.view.element.ElementComponent;
import net.wangit.view.unit.UnitComponent;
import net.wangit.view.unit.UnitFactory;

public final class UnitElement extends ElementComponent {

	private static final ElementWriter writer = new UnitElementWriter();
	protected String unitId;

	public UnitElement() {
	}

	protected void configure(Row row) {
		unitId = row.get("r");
	}

	protected ElementWriter getWriter() {
		return writer;
	}

	protected void cloneAfter(ActionContext ac, Page page) {
		UnitComponent uc = (UnitComponent) getObject();
		if (uc != null) {
			uc.setParent(this);
			setObject(uc.cloneUnit(ac, page));
		} else if (!Checker.isEmpty(unitId)) {
			String domain = getParent().getDomain();
			uc = UnitFactory.getUnit(ac, domain, unitId, page);
			if (uc != null) {
				uc.setParent(this);
				setObject(uc);
			}
		}
	}

}
